//
//  YJFlagView.m
//  test
//
//  Created by crj on 2016/12/22.
//  Copyright © 2016年 crj. All rights reserved.
//

#import "YJFlagView.h"

@interface YJFlagView()

@property (nonatomic, strong) NSString *text;

@property (nonatomic, strong) UIColor *flagColor;

@property (nonatomic, assign) CGPoint origin;
//是否靠左
@property (nonatomic, assign) BOOL isLeft;


@end

static CGFloat const fontSize   = 12.0;
static CGFloat const flagHeight = 12.0;
static CGFloat const space      = 1.0;
static CGFloat const angleWidth = 0.0;

@implementation YJFlagView

- (instancetype)initString:(NSString *)string
                 flagColor:(UIColor *)flagColor
                    origin:(CGPoint)origin
                    isLeft:(BOOL)isLeft
{
    self = [super init];
    if (self) {
        _flagColor  = flagColor;
        _origin     = origin;
        _text       = string;
        _isLeft     = isLeft;
        
        self.backgroundColor = [UIColor clearColor];
        [self reloadContent];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setConfig];
    }
    return self;
}

- (void)setConfig{
    
    _flagColor  = HEXCOLOR(kColorBlue1, 1);
    _origin     = CGPointMake(0, kSpaceSide);
    _text       = @"";
    _isLeft     = YES;

}



- (void)reloadWithText:(NSString *)text color:(UIColor *)color{
    
    _text = text;
    _flagColor = color;
    
    [self reloadContent];
}

- (void)reloadContent{
    
    if(_text.length==0){
        self.hidden = YES;
    }else{
        self.hidden = NO;
    }
    
    CGFloat stringWidth = [self widthOfString:_text font:[UIFont systemFontOfSize:fontSize] height:flagHeight];
    CGFloat superviewWidth = self.superview.frame.size.width;
    
    CGFloat flagWidth = space*2+stringWidth+angleWidth;
    
    if(_isLeft){
        self.frame = CGRectMake(_origin.x, _origin.y, flagWidth, flagHeight);
    }else{
        self.frame = CGRectMake(superviewWidth-flagWidth-_origin.x, _origin.y, flagWidth, flagHeight);
        
    }
    
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {

    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGContextClearRect(contextRef, rect);
    
    [_flagColor set];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = 0.5;
    path.lineCapStyle = kCGLineCapSquare;//线条拐角
    path.lineJoinStyle = kCGLineCapSquare;//终点处理
    
    [path moveToPoint:CGPointMake(0.0, 0.0)];
    [path addLineToPoint:CGPointMake(rect.size.width, 0.0)];
    [path addLineToPoint:CGPointMake(rect.size.width-angleWidth, flagHeight/2)];
    [path addLineToPoint:CGPointMake(rect.size.width, flagHeight)];
    [path addLineToPoint:CGPointMake(0.0, flagHeight)];
    [path closePath];
    [path stroke];//Draws line
    [path fill];//填充颜色
    
    
    CGFloat textHeight = [self heightOfString:_text font:[UIFont systemFontOfSize:fontSize] width:CGFLOAT_MAX];
    
    NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                 NSFontAttributeName:[UIFont systemFontOfSize:fontSize]
                                 };
    
    [_text drawInRect:CGRectMake(space, (self.frame.size.height-textHeight)/2, self.frame.size.width-space*2-angleWidth, textHeight) withAttributes:attributes];
    

}

//#pragma mark - GET
//
//- (void)setText:(NSString *)text{
//    
//    _text = text;
//    
//   
//}





#pragma mark - Private

- (CGFloat)widthOfString:(NSString *)string font:(UIFont *)font height:(CGFloat)height
{
    NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName];
    CGRect rect=[string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return rect.size.width;
}


- (CGFloat)heightOfString:(NSString *)string font:(UIFont *)font width:(CGFloat)width{
    
    CGRect rect;
    
    NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName];
    rect=[string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    
    return rect.size.height;
    
}




@end
